{% extends 'base.html' %}
{% load static %}

{% block title %}Excel处理器状态 - CFS Web System{% endblock %}

{% block page_type %}excel-processor-status{% endblock %}

{% block breadcrumb_items %}
<li class="breadcrumb-item"><a href="{% url 'dashboard:dashboard' %}">仪表板</a></li>
<li class="breadcrumb-item active">Excel处理器状态</li>
{% endblock %}

{% block content %}
<!-- 统计概览 -->
<div class="row mb-4">
    <div class="col-md-3">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4 class="mb-0">{{ stats.total_tasks }}</h4>
                        <p class="mb-0">总任务数</p>
                    </div>
                    <div class="align-self-center">
                        <i class="fas fa-tasks fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card bg-success text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4 class="mb-0">{{ stats.completed_tasks }}</h4>
                        <p class="mb-0">已完成</p>
                    </div>
                    <div class="align-self-center">
                        <i class="fas fa-check-circle fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card bg-warning text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4 class="mb-0">{{ stats.processing_tasks }}</h4>
                        <p class="mb-0">处理中</p>
                    </div>
                    <div class="align-self-center">
                        <i class="fas fa-spinner fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card bg-danger text-white">
            <div class="card-body">
                <div class="d-flex justify-content-between">
                    <div>
                        <h4 class="mb-0">{{ stats.failed_tasks }}</h4>
                        <p class="mb-0">失败</p>
                    </div>
                    <div class="align-self-center">
                        <i class="fas fa-exclamation-triangle fa-2x"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 最近任务 -->
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="fas fa-history me-2"></i>最近任务
                </h5>
            </div>
            <div class="card-body">
                {% if recent_tasks %}
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>任务ID</th>
                                    <th>模板</th>
                                    <th>状态</th>
                                    <th>创建时间</th>
                                    <th>完成时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for task in recent_tasks %}
                                <tr>
                                    <td>
                                        <code>{{ task.id|truncatechars:8 }}</code>
                                    </td>
                                    <td>
                                        {% if task.template %}
                                            <a href="{% url 'template_management:template_detail' task.template.id %}">
                                                {{ task.template.name }}
                                            </a>
                                        {% else %}
                                            <span class="text-muted">未知模板</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if task.status == 'completed' %}
                                            <span class="badge bg-success">已完成</span>
                                        {% elif task.status == 'failed' %}
                                            <span class="badge bg-danger">失败</span>
                                        {% elif task.status == 'processing' %}
                                            <span class="badge bg-warning">处理中</span>
                                        {% else %}
                                            <span class="badge bg-secondary">{{ task.status }}</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <small>{{ task.created_at|date:"Y-m-d H:i:s" }}</small>
                                    </td>
                                    <td>
                                        {% if task.completed_at %}
                                            <small>{{ task.completed_at|date:"Y-m-d H:i:s" }}</small>
                                        {% else %}
                                            <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="btn-group btn-group-sm">
                                            <a href="{% url 'data_processing:task_detail' task.id %}" 
                                               class="btn btn-outline-primary" title="查看详情">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if task.status == 'completed' and task.result_file %}
                                                <a href="{% url 'data_processing:download_result' task.id %}" 
                                                   class="btn btn-outline-success" title="下载结果">
                                                    <i class="fas fa-download"></i>
                                                </a>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                {% else %}
                    <div class="text-center py-4">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <p class="text-muted">暂无任务记录</p>
                        <a href="{% url 'data_processing:data_generation' %}" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>创建新任务
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 自动刷新页面数据
setInterval(function() {
    // 只刷新处理中的任务状态
    const processingTasks = document.querySelectorAll('.badge.bg-warning');
    if (processingTasks.length > 0) {
        location.reload();
    }
}, 30000); // 30秒刷新一次
</script>
{% endblock %}